package com.wm.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wm.bean.ResMenu;
import com.wm.comm.Constant;
import com.wm.dao.ResMenuDAO;

/**
 * @author yangbo
 * 
 * 2012-12-27
 *
 */
public class ResMenuHibernateDAO extends HibernateDaoSupport implements ResMenuDAO {
	
	private static final String LOAD_BY_ID = "from ResMenu where id = ?";
	
	/**
	 * 添加
	 * @param r
	 * @return
	 */
	public ResMenu saveResMenu(ResMenu r){
		this.getHibernateTemplate().save(r);
		return r;
	}
	
	/**
	 * 修改
	 * @param r
	 * @return
	 */
	public ResMenu updateResMenu(ResMenu r){
		this.getHibernateTemplate().update(r);
		return r;
	}
	
	/**
	 * 删除
	 * @param r
	 */
	public void removeResMenu(ResMenu r){
		this.getHibernateTemplate().delete(r);
	}
	
	/**
	 * 根据ID查询
	 * @param id
	 * @return
	 */
	public ResMenu findResMenuById(long id){
		Object[] obj={new Long(id)};
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,obj);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (ResMenu) l.get(0);
		}
	}
	
	/**
	 * 根据饭店ID查询
	 * @param resId
	 * @param isShow
	 * @return
	 */
	public List findResMenuByResIdIsShow(final long resId,final int isShow){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(ResMenu.class);
				c.add(Restrictions.eq("resId", new Long(resId)));
				c.add(Restrictions.eq("isShow", new Integer(isShow)));
				c.addOrder(Order.asc("orderNum"));
				return c.list();
			}
		});
	}
	
	/**
	 * 根据菜名首字母和是否显示查询
	 * @param menuInitials
	 * @param isShow
	 * @return
	 */
	public List findResMenuByMenuInitialsIsShow(final String menuInitials,final int isShow){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(ResMenu.class);
				c.add(Restrictions.eq("menuInitials", menuInitials));
				c.add(Restrictions.eq("isShow", new Integer(isShow)));
				c.addOrder(Order.asc("orderNum"));
				return c.list();
			}
		});
	}
	
	/**
	 * 根据菜名的简拼和是否显示查询
	 * @param menuAnglic
	 * @param isShow
	 * @return
	 */
	public List findResMenuByMenuAnglicIsShow(final String menuAnglic,final int isShow){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(ResMenu.class);
				c.add(Restrictions.eq("menuAnglic", menuAnglic));
				c.add(Restrictions.eq("isShow", new Integer(isShow)));
				c.addOrder(Order.asc("orderNum"));
				return c.list();
			}
		});
	}
	
	/**
	 * 根据菜名的全拼和是否显示查询
	 * @param menuPhonetic
	 * @param isShow
	 * @return
	 */
	public List findMenuByMenuPhoneticIsShow(final String menuPhonetic,final int isShow){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(ResMenu.class);
				c.add(Restrictions.eq("menuPhonetic", menuPhonetic));
				c.add(Restrictions.eq("isShow", new Integer(isShow)));
				c.addOrder(Order.asc("orderNum"));
				return c.list();
			}
		});
	}
	
	/**
	 * 根据菜名和是否显示查询
	 * @param menuName
	 * @param isShow
	 * @return
	 */
	public List findMenuByMenuNameIsShow(final String menuName,final int isShow){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(ResMenu.class);
				c.add(Restrictions.eq("menuName", menuName));
				c.add(Restrictions.eq("isShow", new Integer(isShow)));
				c.addOrder(Order.asc("orderNum"));
				return c.list();
			}
		});
	}
	
	/**
	 * 根据饭店ID、类型ID、是否显示查询
	 * @param resId
	 * @param typeId
	 * @param isShow
	 * @return
	 */
	public List findMenuByResIdTypeIdIsShow(final long resId,final long typeId,final int isShow){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(ResMenu.class);
				c.add(Restrictions.eq("resId", new Long(resId)));
				c.add(Restrictions.eq("typeId", new Long(typeId)));
				c.add(Restrictions.eq("isShow", new Integer(isShow)));
				c.addOrder(Order.asc("orderNum"));
				return c.list();
			}
		});
	}
	
	/**
	 * 根据饭店ID、是否推荐、是否显示查询
	 * @param resId
	 * @param isPromotion
	 * @param isShow
	 * @return
	 */
	public List findMenuByResIdIsPromotionIsShow(final long resId,final int isPromotion,final int isShow){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(ResMenu.class);
				c.add(Restrictions.eq("resId", new Long(resId)));
				c.add(Restrictions.eq("isPromotion", new Integer(isPromotion)));
				c.add(Restrictions.eq("isShow", new Integer(isShow)));
				c.addOrder(Order.asc("orderNum"));
				return c.list();
			}
		});
	}
	
	/**
	 * 根据参数获取菜单对象列表
	 * @param menuName
	 * @param typeId
	 * @param isShow
	 * @param orderby
	 * @param ascOrDesc
	 * @param firstResult
	 * @param maxResults
	 * @return List
	 */
	public List findResMenus(long resId,String menuName,long typeId,int price,int isShow,String orderby,int ascOrDesc,final int firstResult,final int maxResults){
		StringBuffer sb = new StringBuffer();
		sb.append("from ResMenu where 1=1");
		if (resId != -1) {
			sb.append(" and resId=");
			sb.append(resId);
		}
		if(StringUtils.isNotBlank(menuName)){
			sb.append(" and");
			sb.append(" menuName like '%");
			sb.append(menuName);
			sb.append("%'");
	    }
		if (typeId != -1) {
			sb.append(" and typeId=");
			sb.append(typeId);
		}
		if (price != -1) {
			sb.append(" and price=");
			sb.append(price);
		}
		if (isShow != -1) {
			sb.append(" and isShow=");
			sb.append(isShow);
		}
		if (StringUtils.isNotBlank(orderby)) {
			if (ascOrDesc == Constant.ORDER_ASC) {
				sb.append(" order by " + orderby+" asc");
			}
			if (ascOrDesc == Constant.ORDER_DESC) {
				sb.append(" order by " + orderby+" desc");
			}
		}
		final String sql = sb.toString();
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException, SQLException {
				Query query = s.createQuery(sql);
				query.setFirstResult(firstResult);
				query.setMaxResults(maxResults);
				List list = query.list();
				return list;
			}
		});
	}
	
	/**
	 * 根据参数获取菜单对象个数
	 * @param menuName
	 * @param typeId
	 * @param isShow
	 * @return long
	 */
	public long getResMenuNum(long resId,String menuName,long typeId,int price,int isShow){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(id) from ResMenu where 1=1");
		if (resId != -1) {
			sb.append(" and resId=");
			sb.append(resId);
		}
		if(StringUtils.isNotBlank(menuName)){
			sb.append(" and");
			sb.append(" menuName like '%");
			sb.append(menuName);
			sb.append("%'");
	    }
		if (typeId != -1) {
			sb.append(" and typeId=");
			sb.append(typeId);
		}
		if (price != -1) {
			sb.append(" and price=");
			sb.append(price);
		}
		if (isShow != -1) {
			sb.append(" and isShow=");
			sb.append(isShow);
		}
		List l = this.getHibernateTemplate().find(sb.toString());
		
		if (l == null || l.isEmpty()) {
			return 0;
		} else {
			return (Long) l.get(0);
		}
	}
}
